"use strict";
const { Service } = require("egg");
const { Grade } = require("../entity/myentity");
class gradeService extends Service{
  /**
   * 添加一个成绩
   * @param {Grade} grade 
   */
  async add(grade){
    const {app,ctx} = this
    try {
      let query = await app.mysql.get("grade",{u_id:grade.u_id,e_id:grade.e_id})
      if(query != null){
        ctx.logger.error(`用户${grade.u_id}在考试${grade.e_id}已经有成绩`)
        return {suc:false,msg:"该用户在该考试已经有成绩！"}
      }
      let insert = await app.mysql.insert("grade",grade);
      let id = insert.insertId;
      ctx.logger.info(`为用户${grade.u_id}添加考试${grade.e_id}的成绩${id}`);
      return {suc:true,msg:"添加成绩成功！"}
    } catch (error) {
      ctx.logger.error(error)
      return {suc:false,msg:error}
    }
  }
  
  async get(grade){
    const {app,ctx} = this
    try {
      let query = await app.mysql.select("grade",{where:grade});
      ctx.logger.info(`查询一些成绩${query}`)
      return {suc:true,msg:"查询一些成绩成功！",data:query}
    } catch (error) {
      ctx.logger.error(error)
      return {suc:false,msg:error}
    }
  }

  async getOne(grade){
    const {app,ctx} = this
    try {
      let query = await app.mysql.get("grade",grade);
      ctx.logger.info(`查询成绩${query}`)
      return {suc:true,msg:"查询成绩成功！",data:query}
    } catch (error) {
      ctx.logger.error(error)
      return {suc:false,msg:error}
    }
  }

  /**
   * 更新成绩
   * @param {Grade} grade
   */
  async updateGrade(grade){
    const {app} = this
    try {
      await app.mysql.update("grade",grade);
      app.logger.info("更新了成绩"+grade.id)
      return {suc:true,msg:"更新成绩成功"}
    }catch (e){
      app.logger.error(e)
      return {suc:false,msg:e}
    }
  }

}

module.exports = gradeService;